package cn.iocoder.yudao.module.xhyy.service.exam;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.module.xhyy.controller.app.exam.vo.AppExamPageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.xhyy.controller.admin.exam.vo.*;
import cn.iocoder.yudao.module.xhyy.dal.dataobject.exam.ExamDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.xhyy.dal.mysql.exam.ExamMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.sypz.enums.ErrorCodeConstants.EXAM_NOT_EXISTS;

/**
 * 试卷资源 Service 实现类
 *
 * @author duanxinli
 */
@Service
@Validated
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamMapper examMapper;

    @Override
    public String createExam(ExamSaveReqVO createReqVO) {
        // 插入
        ExamDO exam = BeanUtils.toBean(createReqVO, ExamDO.class);
        exam.setId(IdUtil.fastSimpleUUID());
        examMapper.insert(exam);
        // 返回
        return exam.getId();
    }

    @Override
    public void updateExam(ExamSaveReqVO updateReqVO) {
        // 校验存在
        validateExamExists(updateReqVO.getId());
        // 更新
        ExamDO updateObj = BeanUtils.toBean(updateReqVO, ExamDO.class);
        examMapper.updateById(updateObj);
    }

    @Override
    public void deleteExam(String id) {
        // 校验存在
        validateExamExists(id);
        // 删除
        examMapper.deleteById(id);
    }

    private void validateExamExists(String id) {
        if (examMapper.selectById(id) == null) {
            throw exception(EXAM_NOT_EXISTS);
        }
    }

    @Override
    public ExamDO getExam(String id) {
        return examMapper.selectById(id);
    }

    @Override
    public PageResult<ExamDO> getExamPage(ExamPageReqVO pageReqVO) {
        return examMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<ExamDO> getExamPage(AppExamPageReqVO pageReqVO) {
        return examMapper.selectPage(pageReqVO);
    }

}